package cn.wang.web.shiro;

import cn.wang.utils.Encrypt;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * 加密密码 做比较
 * Created by john on 2017/11/3.
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

    boolean result=false;

        //1取到表单数据 名字 密码
        UsernamePasswordToken uptoken=(UsernamePasswordToken)token;
         String userName=uptoken.getUsername();
         String password= new String(uptoken.getPassword());
        //取到数据库数据 密码
        String dbpassword= (String) info.getCredentials();
        //3将表单数据加密
          password= Encrypt.md5(password,userName);
        //4 判断数据 验证是否相等
         result= password.equals(dbpassword);
        return result;
    }
}
